
function getHttpObject(){
    var xmlHttp = false;
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlHttp=false;
        }
    }
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

function validateEditPro(form){
    // Create variable to get input value
    var check_name = form.txtProductname.value;
    var check_price = form.txtPrice.value;
    var check_descrip = form.txtDescrip.value;
    var check_number = form.txtNumber.value;
    var check_from = form.txtFrom.value;

    var errors = [];

    if(check_name==""){
        document.getElementById("idProductname").innerHTML="Please input Product name!";
        form.txtProductname.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idProductname").innerHTML="";
    }

    if(check_price==""){
        document.getElementById("idPrice").innerHTML="Please input the Sell price!";
        form.txtPrice.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idPrice").innerHTML="";
    }

    if(check_descrip==""){
        document.getElementById("idDescrip").innerHTML="Please input the Description!";
        form.txtFrom.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idDescrip").innerHTML="";
    }

    if(check_number==""){
        document.getElementById("idNumber").innerHTML="Please input the Quantity of product!";
        form.txtNumber.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idNumber").innerHTML="";
    }

    if(check_from==""){
        document.getElementById("idFrom").innerHTML="Please input the Country!";
        form.txtFrom.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idFrom").innerHTML="";
    }

    if(errors.length>0){
        return false;
    }
    return true;
}

function addNewProduct(form){
    // Create variable to get input value
    var check_name = form.txtProductname.value;
    var check_price = form.txtPrice.value;
    var check_descrip = form.txtDescrip.value;
    var check_image = form.txtImage.value;
    var check_number = form.txtNumber.value;
    var check_from = form.txtFrom.value;

    var errors = [];

    if(check_name==""){
        document.getElementById("idProductname").innerHTML="Please input Product name!";
        form.txtProductname.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idProductname").innerHTML="";
    }

    if(check_price==""){
        document.getElementById("idPrice").innerHTML="Please input the Sell price!";
        form.txtPrice.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idPrice").innerHTML="";
    }

    if(check_descrip==""){
        document.getElementById("idDescrip").innerHTML="Please input the Description!";
        form.txtDescrip.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idDescrip").innerHTML="";
    }

    if(check_image==""){
        document.getElementById("idImage").innerHTML="Please choose the Image!";
        form.txtImage.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idImage").innerHTML="";
    }

    if(check_number==""){
        document.getElementById("idNumber").innerHTML="Please input the Quantity of product!";
        form.txtNumber.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idNumber").innerHTML="";
    }

    if(check_from==""){
        document.getElementById("idFrom").innerHTML="Please input the Country!";
        form.txtFrom.focus();
        errors[errors.length]="1";
    } else {
        document.getElementById("idFrom").innerHTML="";
    }

    if(errors.length>0){
        return false;
    }
    return true;
}

function checkPrice(price){
    var price_rule= /^[0-9]{4,10}$/;
    if(price==""){
        document.getElementById("idPrice").innerHTML="Please input the Sell price!";
        document.getElementById("btSubmit").disabled=true;
    } else if(!price_rule.test(price)){
        document.getElementById("idPrice").innerHTML="Wrong format. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else {
        document.getElementById("idPrice").innerHTML="";
        document.getElementById("btSubmit").disabled=false;
    }
}

function checkNumber(number){
    var number_rule= /^[0-9]{1,10}$/;
    if(number==""){
        document.getElementById("idNumber").innerHTML="Please input the Quantity of product!";
        document.getElementById("btSubmit").disabled=true;
    } else if(!number_rule.test(number)){
        document.getElementById("idNumber").innerHTML="Wrong format. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else {
        document.getElementById("idNumber").innerHTML="";
        document.getElementById("btSubmit").disabled=false;
    }
}

function checkFrom(from){
    var from_rule= /^[a-zA-Z- ]{1,30}$/;
    if(from==""){
        document.getElementById("idFrom").innerHTML="Please input the Country!";
        document.getElementById("btSubmit").disabled=true;
    } else if(from.length>20){
        document.getElementById("idFrom").innerHTML="Name must be less than 30 characters. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else if(!from_rule.test(from)){
        document.getElementById("idFrom").innerHTML="Wrong format. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else {
        document.getElementById("idFrom").innerHTML="";
        document.getElementById("btSubmit").disabled=false;
    }
}

function checkImage(image){
    if(image==""){
        document.getElementById("idImage").innerHTML="Please choose the Image!";
        document.getElementById("btSubmit").disabled=true;
    } else {
        document.getElementById("idImage").innerHTML="";
        document.getElementById("btSubmit").disabled=false;
    }
}

function checkDescrip(descrip){
    var descrip_rule= /^[a-zA-Z- ]{1,500}$/;
    if(descrip==""){
        document.getElementById("idDescrip").innerHTML="Please input the Description!";
        document.getElementById("btSubmit").disabled=true;
    } else if(descrip.length>20){
        document.getElementById("idDescrip").innerHTML="Name must be less than 500 characters. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else if(!descrip_rule.test(descrip)){
        document.getElementById("idDescrip").innerHTML="Wrong format. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else {
        document.getElementById("idDescrip").innerHTML="";
        document.getElementById("btSubmit").disabled=false;
    }
}

function checkName(name){
    var name_rule= /^[a-zA-Z-_0-9 ]{6,60}$/;
    if(name==""){
        document.getElementById("idProductname").innerHTML="Please input Product name!";
        document.getElementById("btSubmit").disabled=true;
    } else if(name.length<6){
        document.getElementById("idProductname").innerHTML="Name must be more than 5 characters. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else if(name.length>60){
        document.getElementById("idProductname").innerHTML="Name must be less than 60 characters. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    } else if(!name_rule.test(name)){
        document.getElementById("idProductname").innerHTML="Wrong format. Please input again!";
        document.getElementById("btSubmit").disabled=true;
    }
    else if(name.test(checkProductname())){
        document.getElementById("idProductname").innerHTML="";
        document.getElementById("btSubmit").disabled=true;
    } 
    else {
        document.getElementById("idProductname").innerHTML="";
        document.getElementById("btSubmit").disabled=false;
    }
}

function checkProductname(){
    xmlHttp=getHttpObject();
    if(xmlHttp){
        var name = document.getElementById("txtProductname").value;

        xmlHttp.open("POST","AdminController", true);

        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

        var url="btAction=checkProductName&txtname="+name;

        xmlHttp.send(url);

        xmlHttp.onreadystatechange = confirmCheckProductName;

    }
}

function confirmCheckProductName(){
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){

            if(xmlHttp.responseText=="" ){
                document.getElementById("idProductname").innerHTML= xmlHttp.responseText;
                document.getElementById("btSubmit").disabled=false;

            }else{
                document.getElementById("idProductname").innerHTML= xmlHttp.responseText;
                document.getElementById("btSubmit").disabled=true;
            }
        }
    }
}